package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class TestUpdate {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java72db?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&allowMultiQueries=true";
        Connection con = null;
        Statement st = null;
        //update subject set =xxx where id=
        Scanner in=new Scanner(System.in);
        System.out.println("请输入课程的名称，学时，年级和编号：");
        String newName=in.nextLine();
        //?newName=newName.replaceAll("'","\\'");

        int classhour=in.nextInt();
        long grade=in.nextLong();
        long id=in.nextLong();

        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(con.getClass().getName());
            st = con.createStatement();
            System.out.println(st.getClass().getName());
            String sql="update s_subject set subjectname='"+newName+"',classhour="+classhour+",gradeid="+grade+" where id="+id;
            System.out.println(sql);
            int res = st.executeUpdate(sql);
            System.out.printf("成功影响了%d行",res);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            try {

                if (st != null)
                    st.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {

                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
